package com.suyang.syt.util;

import java.io.FileOutputStream;
import java.util.Properties;

public class PropertiesUtil {

	private PropertiesUtil() {

	}

	/** 从类路径下读取配置文件 */
	public static Properties getProperties(String file) throws Exception {
		Properties props = new Properties();
		props.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(file));
		return props;
	}

	/** 根据key获取值 */
	public static String get(String file, String key) throws Exception {
		return getProperties(file).getProperty(key);
	}

	/** 增加属性文件值 */
	public static void addProperties(String[] key, String[] value, String file) throws Exception {
		Properties properties = getProperties(file);
		FileOutputStream outputStream = null;
		properties.put(key, value);
		outputStream = new FileOutputStream(file, true);
		properties.store(outputStream, "modify properties file");
		outputStream.close();
	}

	/** 保存属性到文件中 */
	public static void saveProperties(Properties pro, String file) throws Exception {
		if (pro != null) {
			FileOutputStream outputStream = new FileOutputStream(file, false);
			pro.store(outputStream, "modify properties file");
			outputStream.close();
		}
	}

	/** 修改属性文件 */
	public static void updateProperties(String key, String value, String file) throws Exception {
		if (key == null || "".equalsIgnoreCase(key)) {
			return;
		}
		Properties pro = getProperties(file);
		if (pro == null) {
			pro = new Properties();
		}
		pro.put(key, value);
		saveProperties(pro, file);
	}

}
